package cn.xxliang.platapp.common;

import cn.xxliang.platapp.common.SkyErrMsgUtil;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

public class BaseRespWithData<T> implements Serializable {

    //成功
    public static final Integer SUCCESS_CODE = 200;
    public static final String SUCCESS_MSG = "成功";

    public static final String SUCCESS_STR = "{\"code\":200,\"msg\":\"成功\"}";

    @ApiModelProperty(notes = "状态返回码", required = true, example = "200")
    private Integer code;

    @ApiModelProperty(notes = "状态返回信息", required = true, example = "成功")
    private String msg;

    @ApiModelProperty(notes = "数据", example = "{}")
    private T data;

    public boolean success() {

        if (this.getCode() != null && this.getCode().intValue() == SUCCESS_CODE) {
            return true;
        }
        return false;
    }

    public BaseRespWithData() {
        this.code = SUCCESS_CODE;
        this.msg = SUCCESS_MSG;
    }

    public BaseRespWithData(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public BaseRespWithData(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public BaseRespWithData(Integer code) {
        this.code = code;
        this.msg = SkyErrMsgUtil.ins().getMsg(this.code);
    }

    public void setCode(Integer code) {
        this.code = code;
        this.msg = SkyErrMsgUtil.ins().getMsg(this.code);
    }

    /**
     * 给code赋值
     * @param code
     * @return
     */
    public BaseRespWithData code(Integer code) {
        this.code = code;
        this.msg = SkyErrMsgUtil.ins().getMsg(code);
        return this;
    }

    /**
     * 给msg赋值
     * @param msg
     * @return
     */
    public BaseRespWithData msg(String msg) {
        this.msg = msg;
        return this;
    }

    /**
     * 给data赋值
     * @param data
     * @return
     */
    public BaseRespWithData data(T data) {
        this.data = data;
        return this;
    }


    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public BaseRespWithData serviceError() {
        this.setCode(SystemCodeMsg.SERVICE_HYSTRIX_CODE);
        this.setMsg(SystemCodeMsg.SERVICE_HYSTRIX_MSG);
        return this;
    }

    /**
     * 默认的系统异常
     * @return
     */
    public BaseRespWithData systemError() {
        this.setCode(SystemCodeMsg.SYSTEM_ERROR_CODE);
        this.setMsg(SystemCodeMsg.SYSTEM_ERROR_MSG);
        return this;
    }

    /**
     * 系统code msg都是以99开头
     *
     */
    public static class SystemCodeMsg {
        ///系统异常
        public static final Integer SYSTEM_ERROR_CODE = 999999;
        public static final String SYSTEM_ERROR_MSG = "系統异常";
        //服务降级
        public static final Integer SERVICE_HYSTRIX_CODE = 999998;
        public static final String SERVICE_HYSTRIX_MSG = "服务降级";
        //参数校验异常
        public static final Integer PARAMETER_VALIDATE_ERROR_CODE = 999997;
        public static final String PARAMETER_VALIDATE_ERROR_MSG = "参数校验异常";

    }


    public static void main(String[] args) {
    }

}
